package com.blank.black.entity.bo.example;

import com.blank.black.enums.ConditionEnum;
import com.blank.black.enums.LogicEnum;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class CommonExample {
    private static final long serialVersionUID = 1L;
    private final String DEFAULT_SEPARATOR = LogicEnum.AND.getName();

    protected String tableName;

    protected String separator = DEFAULT_SEPARATOR;

    protected List<ExampleCol> colList;

    protected List<ExampleUpdate> updateList;

    protected List<ExampleJoin> joinList;

    protected String orderByClause;

    protected boolean distinct;

    protected List<Criteria> CriteriaGroupList;

    private Integer limit;

    private Integer offset;

    public CommonExample() {
        CriteriaGroupList = new ArrayList<Criteria>();
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }
    public void setTableName(String tableName,String tableAlias) {
        this.tableName = tableName + " " + tableAlias;
    }

    public String getSeparator() {
        return separator;
    }

    public void setSeparator(String separator) {
        this.separator = separator;
    }
    public void setSeparator(LogicEnum logicEnum) {
        this.separator = logicEnum.getName();
    }

    public List<ExampleCol> getColList() {
        return colList;
    }

    public CommonExample addCol(String colName) {
        if ( !Objects.isNull(colName) && colName.length()>0) {
            ExampleCol col = new ExampleCol();
            col.setCol(colName);
            this.addCol(col);
        }
        return this;
    }

    public CommonExample addCol(String tableAlias,String colName ) {
        if ( !Objects.isNull(colName) && colName.length()>0) {
            ExampleCol col = new ExampleCol();
            col.setTableAlias(tableAlias);
            col.setCol(colName);
            this.addCol(col);
        }
        return this;
    }

    public CommonExample addCol(String tableAlias,String colName,String alias ) {
        if ( !Objects.isNull(colName) && colName.length()>0) {
            ExampleCol col = new ExampleCol();
            col.setTableAlias(tableAlias);
            col.setCol(colName);
            col.setAlias(alias);
            this.addCol(col);
        }
        return this;
    }

    public CommonExample addCol(List<String> colNameList) {
        if ( !Objects.isNull(colNameList) && colNameList.size()>0) {
            for (String colName : colNameList) {
                ExampleCol col = new ExampleCol();
                col.setCol(colName);
                this.addCol(col);
            }
        }
        return this;
    }

    public void addCol(ExampleCol col) {
        if(null == colList){
            colList = new ArrayList<>();
        }
        if (!Objects.isNull(col) && !Objects.isNull(col.getCol()) && col.getCol().length()>0) {
            this.colList.add(col);
        }
    }

    public ExampleJoin addJoin(String tableName) {
        ExampleJoin join = new ExampleJoin();
        join.setTableName(tableName);
        join.setAlias(tableName);
        join.setRelationList(new ArrayList<>());
        return this.addJoin(join);
    }

    public ExampleJoin addJoin(String mode,String tableName) {
        ExampleJoin join = new ExampleJoin();
        join.setMode(mode);
        join.setTableName(tableName);
        join.setAlias(tableName);
        join.setRelationList(new ArrayList<>());
        return this.addJoin(join);
    }
    public ExampleJoin addJoin(String mode,String tableName,String alias) {
        ExampleJoin join = new ExampleJoin();
        join.setMode(mode);
        join.setTableName(tableName);
        join.setAlias(alias);
        join.setRelationList(new ArrayList<>());
        return this.addJoin(join);
    }


    public ExampleJoin addJoin(ExampleJoin join) {
        if(null == joinList){
            this.joinList = new ArrayList<>();
        }
        if (!Objects.isNull(join) && join.isFull()) {
            this.joinList.add(join);
        }
        return join;
    }

    public List<ExampleJoin> getJoinList() {
        return joinList;
    }

    public void setJoinList(List<ExampleJoin> joinList) {
        this.joinList = joinList;
    }

    public List<ExampleUpdate> getUpdateList() {
        if (Objects.isNull(updateList)){
            updateList = new ArrayList<>();
        }
        return updateList;
    }

    public void setUpdateList(List<ExampleUpdate> updateList) {
        this.updateList = updateList;
    }

    public void setCriteriaGroupList(List<Criteria> criteriaGroupList) {
        this.CriteriaGroupList = criteriaGroupList;
    }

    public void setOrderByClause(String orderByClause) {
        this.orderByClause = orderByClause;
    }

    public String getOrderByClause() {
        return orderByClause;
    }

    public void setDistinct(boolean distinct) {
        this.distinct = distinct;
    }

    public boolean isDistinct() {
        return distinct;
    }

    public List<Criteria> getCriteriaGroupList() {
        return CriteriaGroupList;
    }

    public void nextGroup(Criteria criteria) {
        CriteriaGroupList.add(criteria);
    }

    public Criteria nextGroup() {
        Criteria criteria = createCriteriaInternal();
        CriteriaGroupList.add(criteria);
        return criteria;
    }

    public Criteria createCriteria() {
        Criteria criteria = createCriteriaInternal();
        if (CriteriaGroupList.size() == 0) {
            CriteriaGroupList.add(criteria);
        }
        return criteria;
    }

    protected Criteria createCriteriaInternal() {
        Criteria criteria = new Criteria();
        return criteria;
    }

    public void clear() {
        CriteriaGroupList.clear();
        tableName = null;
        colList = null;
        joinList = null;
        separator = DEFAULT_SEPARATOR;
        updateList = null;
        orderByClause = null;
        distinct = false;
        limit = null;
        offset = null;

    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setOffset(Integer offset) {
        this.offset = offset;
    }

    public Integer getOffset() {
        return offset;
    }

    protected abstract static class GeneratedCriteria implements Serializable {

        protected List<Criterion> criteria;

        protected GeneratedCriteria() {
            super();
            criteria = new ArrayList<Criterion>();
        }

        public boolean isValid() {
            return criteria.size() > 0;
        }

        public List<Criterion> getAllCriteria() {
            return criteria;
        }

        public List<Criterion> getCriteria() {
            return criteria;
        }

        protected void addCriterion(LogicEnum logicEnum, String property, String condition) {
            if (logicEnum == null || property == null || property.length() <= 0 || condition == null) {
                throw new RuntimeException("condition for " + property + " cannot be null");
            }
            criteria.add(new Criterion(logicEnum, property, condition));
        }

        protected void addCriterion(LogicEnum logicEnum, String property, String condition, Object value) {
            if (logicEnum == null || property == null || property.length() <= 0 || value == null) {
                throw new RuntimeException("Value for " + property + " cannot be null");
            }
            criteria.add(new Criterion(logicEnum, property, condition, value));
        }

        protected void addCriterion(LogicEnum logicEnum, String property, String condition, Object value1, Object value2) {
            if (logicEnum == null || property == null || property.length() <= 0 || value1 == null || value2 == null) {
                throw new RuntimeException("Between values for " + property + " cannot be null");
            }
            criteria.add(new Criterion(logicEnum, condition, property, value1, value2));
        }

        public Criteria isNull( String alias,LogicEnum logicEnum,String property) {
            addCriterion(logicEnum, alias+"."+property, ConditionEnum.ISNULL.getCondition());
            return (Criteria) this;
        }

        public Criteria isNull( LogicEnum logicEnum,String property) {
            addCriterion(logicEnum, property, ConditionEnum.ISNULL.getCondition());
            return (Criteria) this;
        }

        public Criteria isNotNull( String alias, LogicEnum logicEnum,String property) {
            addCriterion(logicEnum, alias+"."+property, ConditionEnum.ISNOTNULL.getCondition());
            return (Criteria) this;
        }

        public Criteria isNotNull( LogicEnum logicEnum,String property) {
            addCriterion(logicEnum, property, ConditionEnum.ISNOTNULL.getCondition());
            return (Criteria) this;
        }

        public Criteria equalTo(String alias, LogicEnum logicEnum,String property, Object value) {
            addCriterion(logicEnum, alias+"."+property, ConditionEnum.EQUAL.getCondition(), value);
            return (Criteria) this;
        }

        public Criteria equalTo( LogicEnum logicEnum,String property, Object value) {
            addCriterion(logicEnum, property, ConditionEnum.EQUAL.getCondition(), value);
            return (Criteria) this;
        }

        public Criteria notEqualTo(String alias, LogicEnum logicEnum,String property, Object value) {
            addCriterion(logicEnum, alias+"."+property, ConditionEnum.NOT_EQUAL.getCondition(), value);
            return (Criteria) this;
        }

        public Criteria notEqualTo( LogicEnum logicEnum,String property, Object value) {
            addCriterion(logicEnum, property, ConditionEnum.NOT_EQUAL.getCondition(), value);
            return (Criteria) this;
        }

        public Criteria greaterThan(String alias, LogicEnum logicEnum,String property, Object value) {
            addCriterion(logicEnum, alias+"."+property, ConditionEnum.GREATER.getCondition(), value);
            return (Criteria) this;
        }
        public Criteria greaterThan( LogicEnum logicEnum,String property, Object value) {
            addCriterion(logicEnum, property, ConditionEnum.GREATER.getCondition(), value);
            return (Criteria) this;
        }

        public Criteria greaterThanOrEqualTo(String alias, LogicEnum logicEnum,String property, Object value) {
            addCriterion(logicEnum, alias+"."+property, ConditionEnum.GREATEREQUAL.getCondition(), value);
            return (Criteria) this;
        }

        public Criteria greaterThanOrEqualTo( LogicEnum logicEnum,String property, Object value) {
            addCriterion(logicEnum, property, ConditionEnum.GREATEREQUAL.getCondition(), value);
            return (Criteria) this;
        }

        public Criteria lessThan(String alias, LogicEnum logicEnum,String property, Object value) {
            addCriterion(logicEnum, alias+"."+property, ConditionEnum.LESS.getCondition(), value);
            return (Criteria) this;
        }
        public Criteria lessThan( LogicEnum logicEnum,String property, Object value) {
            addCriterion(logicEnum, property, ConditionEnum.LESS.getCondition(), value);
            return (Criteria) this;
        }
        public Criteria lessThanOrEqualTo( String alias,LogicEnum logicEnum,String property, Object value) {
            addCriterion(logicEnum, alias+"."+property, ConditionEnum.LESSEQUAL.getCondition(), value);
            return (Criteria) this;
        }

        public Criteria lessThanOrEqualTo( LogicEnum logicEnum,String property, Object value) {
            addCriterion(logicEnum, property, ConditionEnum.LESSEQUAL.getCondition(), value);
            return (Criteria) this;
        }

        public Criteria like(String alias, LogicEnum logicEnum,String property, String value) {
            addCriterion(logicEnum, alias+"."+property, ConditionEnum.LIKE.getCondition(), value);
            return (Criteria) this;
        }
        public Criteria like( LogicEnum logicEnum,String property, String value) {
            addCriterion(logicEnum, property, ConditionEnum.LIKE.getCondition(), value);
            return (Criteria) this;
        }

        public Criteria notLike( String alias,LogicEnum logicEnum,String property, String value) {
            addCriterion(logicEnum, alias+"."+property, ConditionEnum.NOT_LIKE.getCondition(), value);
            return (Criteria) this;
        }

        public Criteria notLike( LogicEnum logicEnum,String property, String value) {
            addCriterion(logicEnum, property, ConditionEnum.NOT_LIKE.getCondition(), value);
            return (Criteria) this;
        }
        public Criteria in( String alias,LogicEnum logicEnum,String property, List<Object> values) {
            addCriterion(logicEnum, alias+"."+property, ConditionEnum.IN.getCondition(), values);
            return (Criteria) this;
        }

        public Criteria in( LogicEnum logicEnum,String property, List<Object> values) {
            addCriterion(logicEnum, property, ConditionEnum.IN.getCondition(), values);
            return (Criteria) this;
        }

        public Criteria notIn(String alias, LogicEnum logicEnum,String property, List<Object> values) {
            addCriterion(logicEnum, alias+"."+property, ConditionEnum.NOT_IN.getCondition(), values);
            return (Criteria) this;
        }
        public Criteria notIn( LogicEnum logicEnum,String property, List<Object> values) {
            addCriterion(logicEnum, property, ConditionEnum.NOT_IN.getCondition(), values);
            return (Criteria) this;
        }

        public Criteria between(String alias, LogicEnum logicEnum,String property, Object value1, Object value2) {
            addCriterion(logicEnum, alias+"."+property, ConditionEnum.BETWEEN.getCondition(), value1, value2);
            return (Criteria) this;
        }
        public Criteria between( LogicEnum logicEnum,String property, Object value1, Object value2) {
            addCriterion(logicEnum, property, ConditionEnum.BETWEEN.getCondition(), value1, value2);
            return (Criteria) this;
        }

        public Criteria notBetween(String alias, LogicEnum logicEnum,String property, Object value1, Object value2) {
            addCriterion(logicEnum, alias+"."+property, ConditionEnum.NOT_BETWEEN.getCondition(), value1, value2);
            return (Criteria) this;
        }
        public Criteria notBetween( LogicEnum logicEnum,String property, Object value1, Object value2) {
            addCriterion(logicEnum, property, ConditionEnum.NOT_BETWEEN.getCondition(), value1, value2);
            return (Criteria) this;
        }




    }

    public static class Criteria extends GeneratedCriteria implements Serializable {

        protected Criteria() {
            super();
        }
    }

    public static class Criterion implements Serializable {
        private String logic;

        private String property;

        private String condition;

        private Object value;

        private Object secondValue;

        private boolean noValue;

        private boolean singleValue;

        private boolean betweenValue;

        private boolean listValue;

        private String typeHandler;

        public String getCondition() {
            return condition;
        }

        public Object getValue() {
            return value;
        }

        public Object getSecondValue() {
            return secondValue;
        }

        public boolean isNoValue() {
            return noValue;
        }

        public boolean isSingleValue() {
            return singleValue;
        }

        public boolean isBetweenValue() {
            return betweenValue;
        }

        public boolean isListValue() {
            return listValue;
        }

        public String getTypeHandler() {
            return typeHandler;
        }

        public String getLogic() {
            return logic;
        }

        public String getProperty() {
            return property;
        }

        protected Criterion(LogicEnum logicEnum, String property, String condition) {
            super();
            this.property = property;
            this.logic = logicEnum.getName();
            this.condition = condition;
            this.typeHandler = null;
            this.noValue = true;
        }

        protected Criterion(LogicEnum logicEnum, String property, String condition, Object value, String typeHandler) {
            super();
            this.logic = logicEnum.getName();
            this.property = property;
            this.condition = condition;
            this.value = value;
            this.typeHandler = typeHandler;
            if (value instanceof List<?>) {
                this.listValue = true;
            } else {
                this.singleValue = true;
            }
        }

        protected Criterion(LogicEnum logicEnum, String property, String condition, Object value) {
            this(logicEnum, property, condition, value, null);
        }

        protected Criterion(LogicEnum logicEnum, String property, String condition, Object value, Object secondValue, String typeHandler) {
            super();
            this.logic = logicEnum.getName();
            this.property = property;
            this.condition = condition;
            this.value = value;
            this.secondValue = secondValue;
            this.typeHandler = typeHandler;
            this.betweenValue = true;
        }

        protected Criterion(LogicEnum logicEnum, String property, String condition, Object value, Object secondValue) {
            this(logicEnum, property, condition, value, secondValue, null);
        }
    }
}

